//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ssm.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.ssm.pojo.TestJson;
import com.ssm.pojo.User;
import com.ssm.service.UserService;
import java.io.IOException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import static com.hbase.ApplicationConfig.CERT_RATE;
import static com.hbase.utils.RandomUtil.testMathRandom1000;

@Controller
@RequestMapping({"/user"})
public class UserController {
    @Resource
    private UserService userService;
    private String pin = "{\"ROOT\":\n    {\"HEADER\":{ \"POOL_ID\":{2位},//必填，值可为空；池编码（与物理位置相关） 字符串类型\n                \"DB_ID\":{2位},//不填，值可为空（空字符串””）；数据库实例编码 字符串类型\n                \"CHANNEL_ID\":{2位}, //必填，值不为空；渠道标识 字符串类型\n                \"USERNAME\":{?位}, //用户口令安全验证时必填，值不为空；用户名 字符串类型\n                \"PASSWORD\":{?位}, //用户口令安全验证时必填，值不为空；口令 字符串类型\n                \"ENDUSRLOGINID\":{32位}, //session验证时必填，值不为空；32位随机码 字符串类型\n\"ENDUSRLOGINKEY\":{?位}, //session验证时必填，值不为空；登录手机号码 字符串类型\n                \"ENDUSRIP\":{?位}, //终端用户IP记录时必填，值可不为空；终端用户的IP地址 字符串类型\n                \"PHONE_NO\":{11位}, //电子渠道必填  手机号码\n                \"REGION_NO\"{4位}, //电子渠道必填   地区编码：如2201\n                “TRACE_ID”:{32位或35位},//必填，服务轨迹标识，值不为空，字符串类型\n                \"PARENT_CALL_ID\":{?位},//父调用ID  服务轨迹标识,字符串类型，电子渠道不填\n                \"KEEP_LIVE\":{?位},//此节点用于路由会话保持的功能，值为IP地址或手机号码\n                 \n                \"ROUTING\":{\n                   \"ROUTE_KEY\":{},//必填，值不为空；//路由类型  字符串类型\n                   \"ROUTE_VALUE\":{}//必填，值不为空；//路由值  字符串类型\n                 }\n              },\n     \"BODY\": { //BODY部分的报文结构需要根据具体的接口服务来确认\n        \"AUTHEN_INFO\": {\n            \"PROVINCE_GROUP\": \"10011\",\n            \"SESSION_ID\": \"v6YTS3CQ5TXQCGVDY6wtywKm9wrzYnbT0yhHTpmpnXhvG8DQDvgg!-1032372148!1387774544954\",\n            \"SER_NAME\": \"\",\n            \"ROUND_AUDIT\": \"Y\",\n            \"DEST_IP\": \"172.21.0.33\",\n            \"DEST_PORT\": \"11001\",\n            \"SRC_PORT\": \"59698\",\n            \"APP_NAME\": \"NGBOSS\",\n            \"APP_ID\": \"2000\",\n            \"FROM_SYS\": \"BOSS\",\n            \"REGION_ID\": \"\",\n            \"REGION_NAME\": \"\",\n            \"LOGIN_NO\": \"\",\n            \"LOGIN_NAME\": \"\",\n            \"LOGIN_PWD\": \"\",\n            \"IP_ADDRESS\": \"\",\n            \"GROUP_ID\": \"\",\n            \"GROUP_NAME\": \"\",\n            \"CLASS_CODE\": \"\",\n            \"CLASS_NAME\": \"\"\n        },\n        \"OPR_INFO\": {\n            \"REGION_ID\": \"27\",\n            \"CHANNEL_TYPE\": \"11\",\n            \"LOGIN_NO\": \"qff135\",\n            \"GROUP_ID\": \"67307\",\n            \"OP_CODE\": \"1110\"\n        },\n        \"BUSI_INFO\": {\n            \"PHONE_NO\": \"\"\n        }\n    }\n     }\n  }\n}\n";
    protected static final Logger logger = Logger.getLogger(UserController.class);
    private static final Map<Integer, String> operatorCodeMap = new HashMap();

    public UserController() {
    }

    @RequestMapping({"/queryUser.do"})
    public String toIndex(HttpServletRequest request, Model model) throws IOException {
//        String userId = request.getParameter("id");
//        String serialNumber = request.getParameter("traceId");
//        String url = request.getParameter("url");
//        String operatorCode = request.getParameter("operatorCode");
//        User user = this.userService.getUserById(userId);
//        model.addAttribute("user", user);
        logger.info("log4j-flume-name-test");
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        TestJson testJson = (TestJson)mapper.readValue("{\"a\":\"1\",\"b\":\"2\",\"c\":\"test\",\"d\":\"true\",\"e\":\"e\"}", TestJson.class);
        logger.debug("a:" + testJson.getA());
        logger.debug("b:" + testJson.getB());
        logger.debug("c:" + testJson.getC());
        logger.debug("d:" + testJson.getD());
        int i=testMathRandom1000();
        if(i% CERT_RATE ==0){
            throw new NullPointerException();
        }
        return "User";
    }

    @RequestMapping({"/queryUser1.do"})
    public String toIndex1(HttpServletRequest request, Model model) throws RemoteException {
        this.userService.selectByPhone("12345678");
        double i = Math.random();

        Jedis jedis = new Jedis("172.18.193.43",32768);
        jedis.set("foo", "bar");
        String value = jedis.get("foo");
//        RedisDataSource.getClient().set("name", "陈浩翔");
        System.out.println(value);
//        RedisPool.getJedis().set("name", "陈浩翔");
//
//        for(int j = 0; j < 100; ++j) {
//            long startTime = System.currentTimeMillis();
//            System.out.println(RedisPool.getJedis().get("name"));
//            logger.warn(System.currentTimeMillis() - startTime);
//        }

        return "User";
    }

    public static String endCheck() {
        logger.debug("endCheck");
        return null;
    }

    @RequestMapping({"/weChat/entry1.do"})
    public String check(String returnUrl, HttpServletResponse response, HttpServletRequest request, String msg_signature, String timestamp, String nonce, String echostr, String sPostData) throws IOException {
        logger.debug("helloworld");
        this.userService.selectByPhone("12345678");
        return "User";
    }

    public static void test(String serialNumber, String url, String operatorCode) {
        url = url + "operatorDeviceLog";
        url = "http://172.21.11.202:8292/apm-api-service/operatorDeviceLog";
        testOperatorDeviceLogGet(url, serialNumber, operatorCode);
        logger.debug("OperatorDeviceLog count : ");
    }

    private static void testOperatorDeviceLogGet(String url, String serialNumber, String operatorCode) {
        if (operatorCode == null || operatorCode.trim().length() == 0) {
            operatorCode = (String)operatorCodeMap.get(getRandom(1, 10));
        }

        List params = new ArrayList();
        params.add(new BasicNameValuePair("traceId", serialNumber));
        params.add(new BasicNameValuePair("busiTraceNo", ""));
        params.add(new BasicNameValuePair("operatorCode", operatorCode));
        params.add(new BasicNameValuePair("clientIp", "172.10." + getRandom(10, 30) + "." + getRandom(1, 250)));
        params.add(new BasicNameValuePair("busiCode", "busi3201"));
        params.add(new BasicNameValuePair("deviceCode", ""));
        params.add(new BasicNameValuePair("deviceName", "交换机"));
        params.add(new BasicNameValuePair("startTime", System.currentTimeMillis() + ""));
        params.add(new BasicNameValuePair("endTime", System.currentTimeMillis() + (long)getRandom(300, 2000) + ""));
        params.add(new BasicNameValuePair("linkName", "未知"));
        int resultCode = getRandom(10, 100) % 4;
        params.add(new BasicNameValuePair("resultCode", resultCode + ""));
        if (resultCode == 0) {
            params.add(new BasicNameValuePair("resultInfo", "插入成功"));
        } else if (resultCode == 1) {
            params.add(new BasicNameValuePair("resultInfo", "失败!"));
        } else if (resultCode == 2) {
            params.add(new BasicNameValuePair("resultInfo", "失败!"));
        } else if (resultCode == 3) {
            params.add(new BasicNameValuePair("resultInfo", "失败!"));
        }

        String paramStr = URLEncodedUtils.format(params, "UTF-8");
        url = url + "?" + paramStr;
        logger.debug("OperatorDeviceLog info : " + url);
        executeGet(url);
    }

    private static int getRandom(int start, int end) {
        return ThreadLocalRandom.current().nextInt(start, end);
    }

    public static void executeGet(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);

        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {
                ;
            }
        } catch (Exception var7) {
            throw new RuntimeException(var7);
        } finally {
            client.getConnectionManager().shutdown();
        }

    }

    @RequestMapping({"/jackson.do"})
    public String jackson(HttpServletRequest request, Model model) throws IOException, ParseException {
        String data = "<?xml version='1.0' encoding='UTF-8'?><student><age>11</age><id>12</id><name>JavaInterviewPoint</name></student>";

        try {
            XmlMapper xmlMapper = new XmlMapper();
            JsonNode jsonNode = xmlMapper.readTree(data.getBytes());
            ObjectMapper objectMapper = new ObjectMapper();
            String value = objectMapper.writeValueAsString(jsonNode);
            logger.debug("*** Converting XML to JSON ***");
            logger.debug(value);
        } catch (JsonParseException var8) {
            var8.printStackTrace();
        } catch (JsonMappingException var9) {
            var9.printStackTrace();
        } catch (IOException var10) {
            var10.printStackTrace();
        }

        return "User";
    }

    static {
        int i = 0;
//        int i = i + 1;
        operatorCodeMap.put(i, "hf0001");
        ++i;
        operatorCodeMap.put(i, "hf0002");
        ++i;
        operatorCodeMap.put(i, "hf0003");
        ++i;
        operatorCodeMap.put(i, "ww0005");
        ++i;
        operatorCodeMap.put(i, "ww0002");
        ++i;
        operatorCodeMap.put(i, "mas0001");
        ++i;
        operatorCodeMap.put(i, "mas0009");
        ++i;
        operatorCodeMap.put(i, "yyx0008");
        ++i;
        operatorCodeMap.put(i, "rqt0001");
        ++i;
        operatorCodeMap.put(i, "klc0001");
    }
}
